# coding: utf-8

from appserver import db_store
from sqlalchemy import or_
from models.entity.tree_model_base import TreeModelBase

"""
部门数据模型
"""


class Department(TreeModelBase):
    __tablename__="departments"
    __bind_key__="main"

    id=db_store.Column(db_store.Integer,primary_key=True,comment="部门编码")    # 针对数据库
    name=db_store.Column(db_store.String(64),nullable=False,unique=True,comment="部门名字")
    code=db_store.Column(db_store.String(64),nullable=False,unique=True,comment="部门编号")    # 针对企业内部管理
    description=db_store.Column(db_store.String(255),comment="部门描述")
    org_id=db_store.Column(db_store.Integer,db_store.ForeignKey("organizations.id"),comment="隶属机构id")
    parent_id=db_store.Column(db_store.Integer,comment="父部门id")
    path=db_store.Column(db_store.String(255),comment="路径")
    depth=db_store.Column(db_store.Integer,nullable=False,comment="深度")
    child_num=db_store.Column(db_store.Integer,nullable=False,comment="子数量",default=0)
    dis_order=db_store.Column(db_store.Integer,comment="显示排序编号")
    status=db_store.Column(db_store.Integer,nullable=False,comment="部门状态")    # 0-正常 1-禁用
    character=db_store.Column(db_store.String(64),comment="部门性质")
    abbreviation=db_store.Column(db_store.String(64),comment="部门简称")
    email = db_store.Column(db_store.String(64), comment="邮箱")
    # extension_number=db_store.Column(db_store.String(16),comment="分机号码")
    phone_number = db_store.Column(db_store.Integer, comment="电话")
    person_in_charge=db_store.Column(db_store.String(64),comment="负责人")

    def __repr__(self):
        return "<Department %s>" %self.name

    @staticmethod
    def get_department_by_name(name):
        # 根据name返回某个部门的实例
        return Department.query.filter_by(name=name).first()

    @staticmethod
    def get_department_by_code(code):
        # 根据code返回某个部门的实例
        return Department.query.filter_by(code=code).first()

    @staticmethod
    def get_department_by_org_id(org_id):
        # 根据机构id返回某个部门的实例
        return Department.query.filter_by(org_id=org_id).first()

    @staticmethod
    def get_departments_by_organization(the_organization):
        # 返回直属于这个机构的部门的列表
        return Department.query.filter_by(org_id=the_organization.id).filter_by(parent_id=0).all()

    @staticmethod
    def get_departments_all_by_organization(the_organization):
        # 返回属于这个机构的部门的列表
        return Department.query.filter_by(org_id=the_organization.id).all()

    @staticmethod
    def get_departments_all_by_organization_id(org_id):
        # 返回属于这个机构的部门的列表
        return Department.query.filter_by(org_id=org_id).all()

    def get_list_all(self):
        # 以列表形式返回所有部门的实例
        return Department.query.order_by("dis_order").all()

    @staticmethod
    def get_first_level_departments():
        # 获取所有第一层的部门
        return Department.query.filter_by(parent_id=0).all()

    @staticmethod
    def get_department_parent(the_department):
        # 以列表形式返回某个部门的子部门
        return Department.query.filter_by(parent_id=the_department.id).all()

    @staticmethod
    def search(query_word):
        data = []
        departments = Department.query.filter(
            or_(Department.code.like('%' + query_word + '%'),
                Department.name.like('%' + query_word + '%'),
                Department.abbreviation.like('%' + query_word + '%'),
                Department.character.like('%' + query_word + '%'),
                Department.person_in_charge.like('%' + query_word + '%'),
                Department.description.like('%' + query_word + '%')
                )).all()
        for obj in departments:
            data.append(obj.to_dict())
        return data
